package com.neuedu.aop;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @Author: MARK
 * @Date: 2019/7/26 19:46
 * @version: 1.0.0
 * @Description:
 */
@Slf4j
public class Aop {
    public void beginMethod(JoinPoint joinPoint) {
        log.info("前置通知");
    }

    public void returnMethod(JoinPoint joinPoint, Object value) {
        log.info("返回值通知，返回值为"+value);
    }

    public void exceptionMethod(JoinPoint joinPoint, Throwable ex) {
        log.info("异常通知, 异常为"+ex.getMessage());
    }

    public void afterMethod(JoinPoint joinPoint){
        log.info("最终通知，不管什么情况都会执行");
    }

    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("环绕通知");
        //获取当前类名
        String clazz = JSON.toJSONString(proceedingJoinPoint.getTarget().getClass().getName());
        //获取方法名
        String method = JSON.toJSONString(proceedingJoinPoint.getSignature().getName());
        //获取方法参数
        String args = JSON.toJSONString(proceedingJoinPoint.getArgs());
        long start = System.currentTimeMillis();
        //执行方法
        Object object = proceedingJoinPoint.proceed();

        long end = System.currentTimeMillis() - start;
        log.info("当前类：" + clazz
                + "\n方法名" + method
                + "\n参数" + args
                + "\n执行时间" + end/1000.0 + "s"
        );
        return object;
    }
}
